﻿using MatTrace;
using MatTrace.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using WorkflowCore.Interface;
using Yhd.Captcha.Core.OtpNet;
using Zt.QmtqDemo.ConfmPlan.Saga;

namespace Zt.QmtqDemo.ConfmPlan
{
    [RemoteService()]
    public class ConfmPlanAppService : QmtqDemoAppService, IConfmPlanAppService
    {
        private readonly IWorkflowController _host;
        private readonly IPersistenceProvider persistence;
        private readonly YhdTotp totp;
        private readonly ITmrbr001AppService tmrbr001App;
        private IConformPlanRepository conformPlanRepository;

        public ConfmPlanAppService(YhdTotp yhdTotp, IWorkflowController workHost, IPersistenceProvider persistenceProvider,
            ITmrbr001AppService tmrbr001Appservice, IConformPlanRepository planRepository)
        {
            totp = yhdTotp;
            _host = workHost;
            persistence = persistenceProvider;
            tmrbr001App = tmrbr001Appservice;
            conformPlanRepository = planRepository;
        }


        public string Popl001_insert(List<ConfmMatDto> input)
        {
            var utcNow = DateTime.UtcNow;
            string confmPlanNo = $"{utcNow.Year}{utcNow.Month}{utcNow.Day}{totp.Generate()}";
            string orderNo = input.FirstOrDefault().OrderNo;
            var initData = new ConfPlanData()
            {
                PlanNo = confmPlanNo,
                PlanId = this.GuidGenerator.Create(),
                MatDtos = input,
                OrderNo = orderNo,
                Code = 0,
                Message = string.Empty
            };
            var workflowId = _host.StartWorkflow("confmplan", initData)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            return workflowId;
        }

        public Tmbr001Result GetPopl001(string workflowId)
        {
            Tmbr001Result tmbr001 = new Tmbr001Result();
            var instance = persistence.GetWorkflowInstance(workflowId)
                .GetAwaiter().GetResult();
            var data = instance.Data as ConfPlanData;
            tmbr001.Code = data.Code;
            tmbr001.Message = data.Message;
            tmbr001.WorkflowStatus = (int)instance.Status;

            return tmbr001;

        }

        public IQueryable<Tmrbr001ConfirmPlanResultDto> GetTmr01_ConfirmPlan(QueryTmrbr001InputDto queryTmrbr001Input)
        {
            MatTrace.Inputs.QueryTmrbr001Input dto = new MatTrace.Inputs.QueryTmrbr001Input() { FacNo = queryTmrbr001Input.FacNo, DeliverDate1 = queryTmrbr001Input.DeliverDate1,
                 DeliverDate2 = queryTmrbr001Input.DeliverDate2, MatNo = queryTmrbr001Input.MatNo, 
                  OrderNo = queryTmrbr001Input.OrderNo, StockNo = queryTmrbr001Input.StockNo };
           var tmr01Result =  tmrbr001App.GetTmr01_ConfirmPlan(dto).ConfigureAwait(false).GetAwaiter().GetResult();
 
            return ObjectMapper.Map<List<Tmrbr001ConfirmPlanDto>, List<Tmrbr001ConfirmPlanResultDto>>(tmr01Result).AsQueryable();
        }
    }
}
